namespace AltNetUK.Models.Inputs
{
    using System;
    using System.Collections.ObjectModel;
    using System.Text;

    public sealed class ValidationResult
    {
        private Collection<ValidationError> _errors;

        public ValidationResult()
        {
            this.Errors = new Collection<ValidationError>();
        }

        public ValidationResult(Collection<ValidationError> errors)
        {
            this.Errors = errors;
        }

        public static implicit operator string(ValidationResult result)
        {
            return (null == result) ? null as string : result.ToString();
        }

        public static implicit operator ValidationResult(string value)
        {
            ValidationResult result = null;

            if (null != value)
            {
                result = new ValidationResult();
                string[] errors = value.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string error in errors)
                {
                    result.Errors.Add(error);
                }
            }

            return result;
        }

        public Collection<ValidationError> Errors
        {
            get
            {
                return this._errors;
            }

            private set
            {
                if (null == value)
                {
                    throw new ArgumentNullException("value");
                }

                this._errors = value;
            }
        }

        public bool IsValid
        {
            get
            {
                return 0 == this.Errors.Count;
            }
        }

        public override string ToString()
        {
            StringBuilder buffer = new StringBuilder();

            foreach (ValidationError error in this.Errors)
            {
                if (0 != buffer.Length)
                {
                    buffer.Append(Environment.NewLine);
                }

                buffer.Append(error);
            }

            return buffer.ToString();
        }
    }
}